package com.ellricky.baselib.ui

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.andr.common.ui.fragment.BaseToolBarFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ResUtil
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.StaffInfo
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.ui.overdata.*
import com.ellricky.baselib.ui.teacher.OverviewAchievementFragment
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEntityCallback
import kotlinx.android.synthetic.main.pd_fragment_school_overview.*

class SchoolOverviewFragment :
    BaseToolBarFragment<Void, BasePresenter<Void>>()/*, AdapterView.OnItemClickListener*/ {

    companion object {
        fun newInstance(bundle: Bundle? = null): SchoolOverviewFragment {
            val fragment = SchoolOverviewFragment()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getContentLayout(): Int =
        R.layout.pd_fragment_school_overview

    override fun initUIView(root: View) {
        super.initUIView(root)
        if (arguments != null) {
            mTvTitle.setText(R.string.pd_school_overview)
        } else {
            mTvTitle.text = "我的业绩"
        }

        mBtnRight.text = "退出"
        mBtnRight.visibility = View.VISIBLE

        val frgList = listOf(
            getOverViewFragment(),
            OverviewAchievementFragment(),
            OverviewRankFragment()
        )

        val tList = ResUtil.getStrArr(context, R.array.TcItem)
            .toList()

        tabLayout.setUp(childFragmentManager, frgList, tList, vp)

        loadDataOnInit()
    }

    private fun getOverViewFragment(): Fragment {
        if (LocalCacheUtil.isTcRole()) {
            return OverviewTeacherDataFragment()
        }
        if (LocalCacheUtil.isConsultantRole()){
            return OverviewConsultantDataFragment()
        }

        if (LocalCacheUtil.isTmRole()){
            return OverviewTmDataFragment()
        }

        if (LocalCacheUtil.isMarketRole()){
            return OverviewMarketDataFragment()
        }
        if (LocalCacheUtil.isPresidentRole()){
            return OverviewPresidentDataFragment()
        }
        return OverviewTeacherDataFragment()
    }

    private fun loadDataOnInit() {
        val url = EAppUtil.getApiHost()
        val paramMap = mutableMapOf(
            "action" to "empDetail",
            "eid" to LocalCacheUtil.getUid(),
            "emp_id" to LocalCacheUtil.getUid(),
        )

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<StaffInfo>(mContext) {
                override fun onSuccess(data: StaffInfo, msg: String) {
                    tvName.text = "姓名：${data.E_Name}"
                    tvTel.text = "联系方式：${data.E_Tel}"
                    tvWorkDate.text = "入职时间：${data.E_CreatTime}"
                    tvBirth.text = "生日：${data.E_birthday}"
                }
            })
    }


    override fun onClickBtnRight() {
        EAppUtil.onInvalidToken(requireActivity(), "")
    }

    override fun onClickView(v: View?) {
        super.onClickView(v)
    }
}
